<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>同源策略</title>
</head>

<body>
  <h1>同源策略</h1>
  <button>点击获取用户数据</button>
  <script type="text/javascript">
    // todo 同源政策:协议/域名/端口号必须完全相同  不同就是跨域
    const btn = document.querySelector('button');

    btn.onclick = function () {
      const x = new XMLHttpRequest();
      // 因为这里满足同源策略,所以url可以简写
      x.open('GET', '/data')
      // 发送
      x.send();
      x.onreadystatechange = function () {
        if (x.readyState == 4) {
          if (x.status >= 200 && x.status < 300) {
            console.log(x.response);
          }
        }
      }

    }
  </script>
</body>

</html>